package cn.com.sd.system.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;

import cn.com.sd.common.util.MyStringUtils;
import cn.com.sd.common.util.Page;
import cn.com.sd.system.model.Privilege;
import cn.com.sd.system.model.Role;
import cn.com.sd.system.service.IPrivilegeService;
import cn.com.sd.system.service.IRoleService;


public class RoleAction {

	HttpServletRequest request = ServletActionContext.getRequest();
	
	@Resource(name = "roleService")
	private IRoleService roleService;
	
	@Resource(name = "privilegeService")
	private IPrivilegeService privilegeService;
	
	private Integer page;
	
	public String show() {
		
		int count = roleService.getRoleCount();
		Page page = new Page(count, this.page);
		List<Role> roles = roleService.list(page);
		
		request.setAttribute("roleList", roles);
		request.setAttribute("page", page);
		return "show";
	}
	
	public String showAdd() {
		request.setAttribute("privilegeList", privilegeService.list(null));
		return "showAdd";
	}
	
	public String add() {
		String remark = request.getParameter("remark");
		String priIds = request.getParameter("privilegeIds");
		String name = request.getParameter("name");
		Role role = new Role();
		role.setName(name);
		role.setPrivilegeIds(priIds);
		role.setRemark(remark);
		role.setCreateTime(System.currentTimeMillis());
		role.setStatus(0);
		boolean flag = roleService.saveOrUpdate(role);
		if (!flag) {
			request.setAttribute(WebCommon.ERR_MSG, "添加失败");
			return "showAdd";
		}
		return show();
	}
	
	public String del() {
		String ids = request.getParameter("ids");
		if (StringUtils.isEmpty(ids)) {
			//MyStringUtils.outputJSONResult("1110", "没有选中记录", null, response);
			return "error";
		}
		
		List<Integer> idList = new ArrayList<Integer>();
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			if (StringUtils.isEmpty(id) || MyStringUtils.translateStringToInteger(id) == -1) {
				continue;
			}
			idList.add(MyStringUtils.translateStringToInteger(id));
		}
		if (CollectionUtils.isEmpty(idList)) {
			//MyStringUtils.outputJSONResult("1110", "没有选中记录", null, response);
			return "error";
		}
		boolean result = true;
		if (idList.size() == 1) {
			result = roleService.del(idList.get(0));
		} else {
			result = roleService.delBanch(idList);
		}
		if (!result) {
			request.setAttribute(WebCommon.ERR_MSG, "删除失败");
		}
		return show();
		//MyStringUtils.outputJSONResult("8888", "删除成功", null, response);
	}
	
	public String showSet() {
		String id = request.getParameter("id");
		if (StringUtils.isEmpty(id) || MyStringUtils.translateStringToInteger(id) <= 0) {
			return "error";
		}
		Role role = roleService.detail(MyStringUtils.translateStringToInteger(id));
		request.setAttribute("role", role);
		List<Privilege> privilegeList = privilegeService.list(null);
		if (!CollectionUtils.isEmpty(role.getPrivileges())) {
			privilegeList.removeAll(role.getPrivileges());
		}
		request.setAttribute("privilegeList", privilegeList);
		return "showSet";
	}
	
	public String set() {
		String id = request.getParameter("id");
		if (StringUtils.isEmpty(id) || MyStringUtils.translateStringToInteger(id) == -1) {
			request.setAttribute(WebCommon.ERR_MSG, "更改失败");
			return "showSet";
		}
		String remark = request.getParameter("remark");
		String priIds = request.getParameter("privilegeIds");
		String name = request.getParameter("name");
		Role role = new Role();
		role.setId(MyStringUtils.translateStringToInteger(id));
		role.setName(name);
		role.setPrivilegeIds(priIds);
		role.setRemark(remark);
		role.setCreateTime(System.currentTimeMillis());
		role.setStatus(0);
		boolean flag = roleService.saveOrUpdate(role);
		if (!flag) {
			request.setAttribute(WebCommon.ERR_MSG, "更改失败");
			return "showSet";
		}
		return show();
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}
}

